package it.polimi.travelDream.web.beans;

import it.polimi.it.TravelDream.ejb.UserMgr;
import it.polimi.it.TravelDream.ejb.eaos.ExcursionDTO;
import it.polimi.it.TravelDream.ejb.eaos.FlightDTO;
import it.polimi.it.TravelDream.ejb.eaos.HotelDTO;
import it.polimi.it.TravelDream.ejb.eaos.PersPackageMgr;
import it.polimi.it.TravelDream.ejb.eaos.PersonalizePackageDTO;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.ToggleEvent;


/**
 * Session Bean implementation class PersonalizePackageBean
 */

@ManagedBean
@RequestScoped
public class PersonalizePackageBean {
	@EJB
	private PersPackageMgr persPackMgr;
	
	@EJB
	private UserMgr userMgr;
	
	private  List<PersonalizePackageDTO> persPackList;
	
   private FlightDTO flightSelected;
	
	private HotelDTO hotelSelected;
	
	public PersPackageMgr getPersPackMgr() {
		return persPackMgr;
	}

	public void setPersPackMgr(PersPackageMgr persPackMgr) {
		this.persPackMgr = persPackMgr;
	}




	private ExcursionDTO excursionSelected;

    private PersonalizePackageDTO selectedPersonalizedPackage;
	
	
	public FlightDTO getFlightSelected() {
		return flightSelected;
	}

	public PersonalizePackageDTO getSelectedPersonalizedPackage() {
		return selectedPersonalizedPackage;
	}

	public void setSelectedPersonalizedPackage(
			PersonalizePackageDTO selectedPersonalizedPackage) {
		this.selectedPersonalizedPackage = selectedPersonalizedPackage;
	}

	public void setFlightSelected(FlightDTO flightSelected) {
		this.flightSelected = flightSelected;
	}

	public HotelDTO getHotelSelected() {
		return hotelSelected;
	}

	public void setHotelSelected(HotelDTO hotelSelected) {
		this.hotelSelected = hotelSelected;
	}

	public ExcursionDTO getExcursionSelected() {
		return excursionSelected;
	}

	public void setExcursionSelected(ExcursionDTO excursionSelected) {
		this.excursionSelected = excursionSelected;
	}

	public List<PersonalizePackageDTO> getPersPackList() {
		return persPackList;
	}

	public void setPersPackList(List<PersonalizePackageDTO> persPackList) {
		this.persPackList = persPackList;
	}

	// prende la lista di pacchetti personalizzati dello user
	@PostConstruct
	public void getPersonalizedPackList() {
		persPackList = new ArrayList<PersonalizePackageDTO>();
		persPackList = persPackMgr.getPersPackList(userMgr.getUserDTO().getEmail());
		//System.out.println("ciao ra **************");
		//System.out.println(persPackList.get(0).getIdbasicproduct1());
	}

	// prende il pacchetto da modificare e cambia il volo
	public void changeFlight(){
		System.out.println("*********ciao******** flight ");
		System.out.println(selectedPersonalizedPackage.getIdbasicproduct1());
		System.out.println(flightSelected.getIdbasicproduct());
		System.out.println(flightSelected.getIdbasicproduct());
		selectedPersonalizedPackage.setIdbasicproduct1(flightSelected.getIdbasicproduct());
		
		saveChanges();
	}
	//cambia l hotel
	public void changeHotel(){
		System.out.println("*********ciao********** hotel ");
		selectedPersonalizedPackage.setIdbasicproduct2(hotelSelected.getIdbasicproduct());
		saveChanges();
	}

	// cambia l'escursione
	public void changeExcursion(){
		System.out.println("*********ciao********** excursions ");
		selectedPersonalizedPackage.setIdbasicproduct3(excursionSelected.getIdbasicproduct());
		saveChanges();
		}

	// salva le modifiche al pacchetto all'interno del database
	public void saveChanges(){
		persPackMgr.changeBasicProduct(selectedPersonalizedPackage, userMgr.getUserDTO().getEmail());
		
	}

	// svuota la lista
	public void clearList(){
		persPackList.clear();
		
	}
		
	 public void onRowToggle(ToggleEvent event) {  
	        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,  
	                                            "Row State " + event.getVisibility(),  
	                                            "IDPackage:" + ((PersonalizePackageDTO) event.getData()).getIdpackage());  
	          
	        FacesContext.getCurrentInstance().addMessage(null, msg);  
	    }  
	

    /**
     * Default constructor. 
     */
    public PersonalizePackageBean() {
        // TODO Auto-generated constructor stub
    }

}
